package com.kgc.web;

import com.kgc.dao.TousuDao;
import com.kgc.entity.Tousu;
import com.kgc.untils.PageUtils;
import com.kgc.vo.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@WebServlet(name = "tousuServlet", value = "/tousuServlet")
public class tousuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String opt = request.getParameter("opt");
        TousuDao tousuDao = new TousuDao();
        if ("list".equals(opt)) {
            String name = request.getParameter("name");
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            PageUtils pageUtils = new PageUtils();
            if (StringUtils.isNotEmpty(page)) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (StringUtils.isNotEmpty(limit)) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            int totalCount = tousuDao.countAll(name);
            pageUtils.init(totalCount);
            //查询数据
            List<Tousu> list = tousuDao.selectAll(pageUtils, name);
            response.getWriter().write(R.success("用户列表加载成功...", totalCount, list).toJSON());
        } else if ("edit".equals(opt)) {
            Date date = new Date();
            long time = date.getTime();
            Timestamp timestamp = new Timestamp(time);
            String id = request.getParameter("id");
            String content = request.getParameter("content");
           String status = request.getParameter("status");
            String result = request.getParameter("result");
            int count = tousuDao.updateTousu(new Tousu(Integer.parseInt(id), content, Integer.parseInt(status),timestamp,result));
            if (count > 0) {
                if (count > 0) {
                    response.getWriter().write(R.success("投诉修改成功!").toJSON());
                } else {
                    response.getWriter().write(R.error("投诉修改失败!").toJSON());
                }
            }
        } else if ("delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = tousuDao.deleteTousu(id);
            if (count > 0) {
                if (count > 0) {
                    response.getWriter().write(R.success("删除成功!").toJSON());
                } else {
                    response.getWriter().write(R.error("删除失败!").toJSON());
                }
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
